package com.lzy.crm.controller;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lzy.crm.base.page.PageQuery;
import com.lzy.crm.base.util.BootStrapReturn;
import com.lzy.crm.base.util.JsonUtil;
import com.lzy.crm.base.util.StringUtil;
import com.lzy.crm.domain.PpUser;
import com.lzy.crm.domain.TradeFlow;
import com.lzy.crm.domain.User;
import com.lzy.crm.service.BankCardService;
import com.lzy.crm.service.PpUserService;
import com.lzy.crm.service.TradeFlowService;
import com.lzy.crm.util.BootJsonResponse;
import com.lzy.crm.util.PpUserConstants;
import com.lzy.crm.util.UserConstants;
import com.lzy.crm.vo.TradeFlowVo;



/**
 * 交易流水
 * @author csp
 *
 */
@Controller
@RequestMapping("/admin")
public class TradeFlowController {
	
	Logger logger= Logger.getLogger(TradeFlowController.class);
	
	@Resource
	TradeFlowService tradeFlowService;
	
	@Resource
	PpUserService ppUserService;
	
	@Resource
	BankCardService bankCardService;
	
	/**
	 * 交易流水管理
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("/tradeFlow")  
	public String tradeFlow(HttpServletRequest request,ModelMap model) {
		return "tradeFlow/tradeFlow_list";
	}

	@RequestMapping(path="/tradeFlow_data",method=RequestMethod.POST,produces="text/html;charset=UTF-8")
	@ResponseBody
	public String noticeListData(HttpServletRequest request, @RequestParam Map<String, String> form,
			ModelMap model) {
		BootStrapReturn<TradeFlowVo> result = new BootStrapReturn<TradeFlowVo>();
		try {
			String gameUserId = request.getParameter("gameUserId");
			String status = request.getParameter("status");
			PpUser user = (PpUser)request.getSession().getAttribute("userSession");
			if(user.getRid() == PpUserConstants.USER_TYPE_MANAGE){
				return null;
			}
			Integer currentPage = Integer.parseInt(request.getParameter("numPerPage"));
			Integer numPerPage = Integer.parseInt(request.getParameter("pageNum"));
			PageQuery pageQuery = new PageQuery(numPerPage,currentPage);
			pageQuery.setIsQueryTotal(true); 
			List<TradeFlowVo> tradeFlowVoList =tradeFlowService.getTradeFlowList(gameUserId,status,pageQuery);
			result.setPage(pageQuery.getPageIndex());
			result.setTotal(pageQuery.getTotalCount());
			result.setRows(tradeFlowVoList);
		} catch (Exception e) {
			logger.error("交易流水列表", e);
		}
		return JsonUtil.toJson(result) ;
	}
	
	/**
	 * 添加交易流水
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("/tradeFlow_edit")  
	public String tradeFlowEdit(HttpServletRequest request,ModelMap model) {
		String userId = request.getParameter("userId");
		PpUser user = ppUserService.getById(userId);
		/*List<BankCard> bankCardList = bankCardService.getBankCardListByUserId(user.getId().toString());
		if(bankCardList!=null&&bankCardList.size()>0){
			model.put("bankCard", bankCardList.get(0));
		}*/
		model.put("user", user);
		return "tradeFlow/tradeFlow_edit";
	}
	
	@RequestMapping(path="/tradeFlow_edit_save",method=RequestMethod.POST,produces="text/html;charset=UTF-8")
	@ResponseBody
	public String tradeFlowSave(HttpServletRequest request,@RequestParam Map<String, String> form,ModelMap model) {
		BootJsonResponse response=new BootJsonResponse();
		try {
			
			User user = (User)request.getSession().getAttribute("userSession");
			if(!StringUtil.equals(user.getUserType(), UserConstants.USER_TYPE_MANAGE)){
				response.setStatusCode(BootJsonResponse.STATUS_CODE_FAILURE);
				response.setMessage(" 权限不足，请联系管理员 ");
				return JsonUtil.toJson(response);
			}
			double amount = Double.parseDouble(form.get("amount"));
			Integer bankCardId = Integer.parseInt(form.get("bankCardId"));
			Integer userId = Integer.parseInt(form.get("userId"));
			String remark = form.get("remark");
			Integer result = tradeFlowService.remitTrade(user, userId, amount, bankCardId, remark);
			if(result==1){
				response.setStatusCode(BootJsonResponse.STATUS_CODE_FAILURE);
				response.setMessage(" 超过所能体现的金额  ");
				JsonUtil.toJson(response);
			}else if(result==0){
				response.setStatusCode(BootJsonResponse.STATUS_CODE_SUCCESS);
				response.setMessage("操作成功~");
			}
		} catch (Exception e) {
			logger.error(" 添加体现记录 ", e);
			response.setStatusCode(BootJsonResponse.STATUS_CODE_FAILURE);
			response.setMessage(" 系统错误，请联系管理员 ");
		}
		return JsonUtil.toJson(response);
	}
	
	
	/**
	 * 查看交易流水
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("/tradeFlow_query")  
	public String tradeFlowQuery(HttpServletRequest request,ModelMap model) {
		String id = request.getParameter("id");
		TradeFlow tradeFlow= tradeFlowService.getById(id);
		PpUser user = ppUserService.getById(tradeFlow.getUid().toString());
		model.put("tradeFlow", tradeFlow);
		model.put("user", user);
		return "tradeFlow/tradeFlow_show";
	}
	
	
	/***************      手机端管理区         ***************/
	
	@RequestMapping("/tradeFlow_mobile")  
	public String tradeFlowMobile(HttpServletRequest request,ModelMap model) {
		return "mobile/tradeFlow/tradeFlow_list";
	}
	
	
	@RequestMapping("/tradeFlow_edit_mobile")  
	public String tradeFlowEditMobile(HttpServletRequest request,ModelMap model) {
		String userId = request.getParameter("userId");
		/*PpUser user = userService.getById(userId);
		List<BankCard> bankCardList = bankCardService.getBankCardListByUserId(user.getId().toString());
		if(bankCardList!=null&&bankCardList.size()>0){
			model.put("bankCard", bankCardList.get(0));
		}
		model.put("user", user);*/
		return "mobile/tradeFlow/tradeFlow_edit";
	}
	
	
	/**
	 * 查看交易流水
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("/tradeFlow_query_mobile")  
	public String tradeFlowQueryMobile(HttpServletRequest request,ModelMap model) {
		String id = request.getParameter("id");
		TradeFlow tradeFlow= tradeFlowService.getById(id);
		PpUser user = ppUserService.getById(tradeFlow.getUid());
		model.put("tradeFlow", tradeFlow);
		model.put("user", user);
		return "mobile/tradeFlow/tradeFlow_show";
	}
	
	/**
	 * 玩家申请提款操作
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("/tradeFlow_apply_mobile")  
	public String tradeFlow_apply_mobile(HttpServletRequest request,ModelMap model) {
		return "mobile/tradeFlow/tradeFlow_apply_list";
	}
	
	/**
	 * 查看交易流水
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("/tradeFlow_apply_query_mobile")  
	public String tradeFlow_apply_query_mobile(HttpServletRequest request,ModelMap model) {
		String id = request.getParameter("id");
		TradeFlow tradeFlow= tradeFlowService.getById(id);
		PpUser user = ppUserService.getById(tradeFlow.getUid());
		model.put("tradeFlow", tradeFlow);
		model.put("user", user);
		return "mobile/tradeFlow/tradeFlow_apply_edit";
	}
	
	@RequestMapping(path="/tradeFlow_apply_save",method=RequestMethod.POST,produces="text/html;charset=UTF-8")
	@ResponseBody
	public String tradeFlow_apply_save(HttpServletRequest request,@RequestParam Map<String, String> form,ModelMap model) {
		BootJsonResponse response=new BootJsonResponse();
		try {
			
			User user = (User)request.getSession().getAttribute("userSession");
			if(!StringUtil.equals(user.getUserType(), UserConstants.USER_TYPE_MANAGE)){
				response.setStatusCode(BootJsonResponse.STATUS_CODE_FAILURE);
				response.setMessage(" 权限不足，请联系管理员 ");
				return JsonUtil.toJson(response);
			}
			String id = request.getParameter("tradeFlowId");
			TradeFlow tradeFlow= tradeFlowService.getById(id);
			tradeFlow.setStatus("1");
			tradeFlowService.updateByPrimaryKeySelective(tradeFlow);
			response.setStatusCode(BootJsonResponse.STATUS_CODE_SUCCESS);
			response.setMessage("操作成功~");
			
		} catch (Exception e) {
			logger.error(" 添加体现记录 ", e);
			response.setStatusCode(BootJsonResponse.STATUS_CODE_FAILURE);
			response.setMessage(" 系统错误，请联系管理员 ");
		}
		return JsonUtil.toJson(response);
	}
	
	
}
